<?php
/**
 * Created by PhpStorm.
 * User: zyh
 * Date: 2018/5/9
 * Time: 上午10:00
 */

namespace app\index\controller;
use app\admin\model\User;
use GatewayClient\Gateway;
use think\Controller;
use think\Session;

class Auth extends Controller
{
    public function login()
    {
        $status = ['stauts'=>1,'error'=>''];
        if ($arr = input('post.')){
            $user = new User();
            if ($user->login($arr)){
                return redirect(url("index/index"));
            }else{
                $status = ['status'=>0,'error'=>'用户名或密码错误'];
            }
        }
        $this->assign("status",$status);
        return view();
    }

    public function logout()
    {
        Session::set('login',false);
        $this->redirect('auth/login');
    }

    public function bind($clientid)
    {
        $id = Session::get("user")['id'];
        //绑定WEBSOCKET的CLIENTID到用户ID
        Gateway::bindUid($clientid,$id);
        Gateway::joinGroup($clientid,"user");
        $data['type'] = "user";
        $data['data'] = Session::get('user');
        $data['data']['score'] = User::get($id)['score'];
        Gateway::sendToClient($clientid,json_encode($data));
    }
}